package com.mihu.stmall.manager.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mihu.stmall.manager.entiy.Category;
import com.mihu.stmall.manager.mapper.CategoryMapper;
import com.mihu.stmall.manager.service.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


//实现类 实现servce层接口  添加service注解
//添加service注解
@Service("CategoryService")
public class CategoryServiceImpl implements CategoryService {
    //自动注入mapper层对象
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int add(Category category) throws Exception {
        //验证参数
        if (category.getName()==null
                ||category.getName().length()==0){
            throw new Exception("分类名不能为空");
        }
        if (category.getRecom()==null){
            throw new Exception("推荐不能为空");
        }
//        逻辑处理

//        访问数据库
        return categoryMapper.insert(category);
    }

    @Override
    public int remove(Integer id) throws Exception {
        Category category = categoryMapper.selectById(id);
        if (category==null){
            throw new Exception("ID不存在");
        }
        return categoryMapper.delete(id);
    }

    @Override
    public int edit(Category category) throws Exception {
        Category sCategory = categoryMapper.selectById(category.getId());
        if (sCategory==null){
            throw new Exception("ID不存在");
        }
        return categoryMapper.update(category);
    }

    @Override
    public Category getById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public List<Category> search(Category category) {
        return categoryMapper.select(category);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Category category) {
        //分三步
        //1 启动分页
        PageHelper.startPage(page,limit);
        //2 调用Mapper层
       List<Category> list = categoryMapper.select(category);
        //3 构造PageInfo
        PageInfo pageInfo = new PageInfo(list);

        return pageInfo;
    }
}
